<?php if (!defined('BASEPATH')) exit('No direct script access allowed'); 

// @TODO: Get results only where they are set to active

class Teacher_model extends CI_Model
{
	protected $_table;	    
	/**
	 * Constructor
	 *
	 * @access	public
	 */
	function __construct()
	{
		parent::__construct();
		$this->load->helper('date');
	}
	
	function get_all_teacher($limit, $start)
	{
		$this->db->select('giaovien.id, giaovien.magv, giaovien.tengv, giaovien.ngaysinh, giaovien.email, giaovien.didong, giaovien.status, khoa.tenkhoa');
		$this->db->from('giaovien','khoa');
		$this->db->join('khoa','khoa.id = giaovien.id_khoa');
		$this->db->order_by('khoa.tenkhoa','asc');
		$this->db->limit($limit, $start);
		$query = $this->db->get();
		return $query->result_array();
	}	
	
	function get_teacher_info($gvid)
	{
		$this->db->select('id, magv, tengv, status');
		$this->db->from('giaovien');
		$this->db->where('id',$gvid);
		$query = $this->db->get();
		return $query->row();
	}
	
	function get_teacher($gvid)
	{
		$this->db->select('*');
		$this->db->from('giaovien');
		$this->db->where('id',$gvid);
		$query = $this->db->get();
		return $query->row();
	}
	
	function get_cong_tac($gvid)
	{
		$this->db->select('*');
		$this->db->from('giaovien_congtac');
		$this->db->where('id_gv',$gvid);
		$query = $this->db->get();
		return $query->result_array();
	}
	
	function get_all_khoa()
	{
		$this->db->select('id, makhoa, tenkhoa, status');
		$this->db->from('khoa');
		$query = $this->db->get();
		return $query->result_array();
	}
	
	function delete_dangky($id)
	{
		$this->db->where('id', $id);
		$this->db->delete('dangkyhoc');
	}

	public function count_all_teacher()
	{
		$this->db->select('id');
		$this->db->from('giaovien');
		return $this->db->count_all_results();
	}
	
	// get all hocvien by current year from db to add to lop
	function get_all_class()
	{
		//$year = date('Y');
		$this->db->select('id, malop, tenlop');
		$this->db->from('lophoc');
		$this->db->order_by('id','desc');
		//$this->db->where('YEAR(date_created)',$year);
		$this->db->where('deleted',0);
		$query = $this->db->get();
		return $query->result_array();
	}
	
	function get_class_of_teacher_by_id($gvid)
	{
		$this->db->select('giaovien.magv, giaovien.tengv, lophoc.id as lophocid, lophoc.malop, lophoc.tenlop, giaovien_lophoc.*');
		$this->db->from('giaovien','giaovien_lophoc','lophoc');
		$this->db->join('giaovien_lophoc','giaovien_lophoc.gv_id = giaovien.id');
		$this->db->join('lophoc', 'lophoc.id = giaovien_lophoc.lop_id');
		$this->db->where('giaovien_lophoc.gv_id',$gvid);

		$query = $this->db->get();
		return $query->result_array();
		
	}
	
	function add_lophoc_to_giaovien($results,$gvid)
	{
		if($this->input->post('listIds') == '')
		{
			$this->delete_lopids($idlop);
		}
		
		else
		{
			$this->delete_lopids($gvid);
			foreach($results as $selected)
			{
				$this->insert_lopids($selected, $gvid);
			}
		}
	}
	
	// xoa id lop tu bang giaovien_lophoc
	function delete_lopids($idgv)
	{
		$this->db->where('gv_id',$idgv);
		$this->db->delete('giaovien_lophoc');
	}
	
	// them lop vao bang giaovien_lophoc
	function insert_lopids($idlop, $idgv)
	{
		$fields = array(
				'gv_id' => $idgv,
				'lop_id'=> $idlop,
		);
		$this->db->set($fields);
		$this->db->insert('giaovien_lophoc');
	}
	
	// Thêm mới giáo viên
	function add_teacher($avatar)
	{
		$tengv = $this->input->post('tengv');
		
		$date1 = str_replace('/', '-', $this->input->post('ngaysinh'));
		$ngaysinh = date('Y-m-d', strtotime($date1));
		
		$date2 = str_replace('/', '-', $this->input->post('ngaytuyendung'));
		$ngaytuyendung = date('Y-m-d', strtotime($date2));
		
		$date3 = str_replace('/', '-', $this->input->post('ngaynhapngu'));
		$ngaynhapngu = date('Y-m-d', strtotime($date3));
		
		$date4 = str_replace('/', '-', $this->input->post('ngayvaodang'));
		$ngayvaodang = date('Y-m-d', strtotime($date4));
		
		$date5 = str_replace('/', '-', $this->input->post('ngaychinhthuc'));
		$ngaychinhthuc = date('Y-m-d', strtotime($date5));
		
		$date6 = str_replace('/', '-', $this->input->post('ngaysinhvochong'));
		$ngaysinhvochong = date('Y-m-d', strtotime($date6));
		
		$email = $this->input->post('email');
		$gioitinh = $this->input->post('gioitinh');
		$didong = $this->input->post('didong');
		$diachi = $this->input->post('diachi');
		$cmnd = $this->input->post('cmnd');
		$stt = $this->input->post('status');
		
		$fields = array(
				'tengv'			=> $tengv,
				'ngaysinh'		=> $ngaysinh,
				'email'			=> $email,
				'gioitinh'		=> $gioitinh,
				'didong'		=> $didong,
				'id_khoa'		=> $this->input->post('makhoa'),
				'diachi'		=> $diachi,
				'cmnd'			=> $cmnd,
				'edited_by' 	=> $this->user->id,
				'tenks' 		=> $this->input->post('tenks'),
				'shsq'			=> $this->input->post('shsq'),
				'capbac'		=> $this->input->post('capbac'),
				'trinhdo'		=> $this->input->post('trinhdo'),
				'nguyenquan' 	=> $this->input->post('nguyenquan'),
				'ngaynhapngu' 	=> $ngaynhapngu,
				'ngaytuyendung' => $ngaytuyendung,
				'ngayvaodang'	=> $ngayvaodang,
				'ngaychinhthuc'	=> $ngaychinhthuc,
				'mucluong'		=> $this->input->post('mucluong'),
				'ngoaingu'		=> $this->input->post('ngoaingu'),
				'suckhoe'		=> $this->input->post('suckhoe'),
				'khenthuong'	=> $this->input->post('khenthuong'),
				'quatruong'		=> $this->input->post('quatruong'),
				'chientruong'	=> $this->input->post('chientruong'),
				'danhhieu'		=> $this->input->post('danhhieu'),
				'kyluat'		=> $this->input->post('kyluat'),
				'dinuocngoai'	=> $this->input->post('dinuocngoai'),
				'nhao'			=> $this->input->post('nhao'),
				'tencha'		=> $this->input->post('tencha'),
				'namsinhcha'	=> $this->input->post('namsinhcha'),
				'nghenghiepcha'	=> $this->input->post('nghenghiepcha'),
				'tenme'			=> $this->input->post('tenme'),
				'namsinhme'		=> $this->input->post('namsinhme'),
				'nghenghiepme'	=> $this->input->post('nghenghiepme'),
				'ghichu1'		=> $this->input->post('ghichu1'),
				'tenchavochong'	=> $this->input->post('tenchavochong'),
				'namsinhchavc'	=> $this->input->post('namsinhchavc'),
				'nghenghiepchavc'	=> $this->input->post('nghenghiepchavc'),
				'tenmevochong'	=> $this->input->post('tenmevochong'),
				'namsinhmevc'	=> $this->input->post('namsinhmevc'),
				'nghenghiepmevc'=> $this->input->post('nghenghiepmevc'),
				'ghichu2'		=> $this->input->post('ghichu2'),
				'tenvochong'	=> $this->input->post('tenvochong'),
				'nghenghiepvochong'	=> $this->input->post('nghenghiepvochong'),
				'ngaysinhvochong'	=> $ngaysinhvochong,
				'choovochong'	=> $this->input->post('choovochong'),
				'concai'		=> $this->input->post('concai'),
				'ghichu3'		=> $this->input->post('ghichu3'),
				'anhdaidien' 	=> $avatar,
		);
		
		$fields2 = array(
				'tengv'			=> $tengv,
				'ngaysinh'		=> $ngaysinh,
				'email'			=> $email,
				'gioitinh'		=> $gioitinh,
				'didong'		=> $didong,
				'id_khoa'		=> $this->input->post('makhoa'),
				'diachi'		=> $diachi,
				'cmnd'			=> $cmnd,
				'edited_by' 	=> $this->user->id,
				'tenks' 		=> $this->input->post('tenks'),
				'shsq'			=> $this->input->post('shsq'),
				'capbac'		=> $this->input->post('capbac'),
				'trinhdo'		=> $this->input->post('trinhdo'),
				'nguyenquan' 	=> $this->input->post('nguyenquan'),
				'ngaynhapngu' 	=> $ngaynhapngu,
				'ngaytuyendung' => $ngaytuyendung,
				'ngayvaodang'	=> $ngayvaodang,
				'ngaychinhthuc'	=> $ngaychinhthuc,
				'mucluong'		=> $this->input->post('mucluong'),
				'ngoaingu'		=> $this->input->post('ngoaingu'),
				'suckhoe'		=> $this->input->post('suckhoe'),
				'khenthuong'	=> $this->input->post('khenthuong'),
				'quatruong'		=> $this->input->post('quatruong'),
				'chientruong'	=> $this->input->post('chientruong'),
				'danhhieu'		=> $this->input->post('danhhieu'),
				'kyluat'		=> $this->input->post('kyluat'),
				'dinuocngoai'	=> $this->input->post('dinuocngoai'),
				'nhao'			=> $this->input->post('nhao'),
				'tencha'		=> $this->input->post('tencha'),
				'namsinhcha'	=> $this->input->post('namsinhcha'),
				'nghenghiepcha'	=> $this->input->post('nghenghiepcha'),
				'tenme'			=> $this->input->post('tenme'),
				'namsinhme'		=> $this->input->post('namsinhme'),
				'nghenghiepme'	=> $this->input->post('nghenghiepme'),
				'ghichu1'		=> $this->input->post('ghichu1'),
				'tenchavochong'	=> $this->input->post('tenchavochong'),
				'namsinhchavc'	=> $this->input->post('namsinhchavc'),
				'nghenghiepchavc'	=> $this->input->post('nghenghiepchavc'),
				'tenmevochong'	=> $this->input->post('tenmevochong'),
				'namsinhmevc'	=> $this->input->post('namsinhmevc'),
				'nghenghiepmevc'=> $this->input->post('nghenghiepmevc'),
				'ghichu2'		=> $this->input->post('ghichu2'),
				'tenvochong'	=> $this->input->post('tenvochong'),
				'nghenghiepvochong'	=> $this->input->post('nghenghiepvochong'),
				'ngaysinhvochong'	=> $ngaysinhvochong,
				'choovochong'	=> $this->input->post('choovochong'),
				'concai'		=> $this->input->post('concai'),
				'ghichu3'		=> $this->input->post('ghichu3'),
		);
		if($avatar==null)
		{
			$this->db->set($fields2);
		}
		else {
			$this->db->set($fields);
		}
		$this->db->insert('giaovien');
		$teacherid = $this->db->insert_id();
		
		$magv = $this->auto_code($teacherid);		
		$data = array(
				'magv' => 'T19CB'.$magv,
		);
		
		$this->db->set($data);
		$this->db->where('id', $teacherid);
		$this->db->update('giaovien');
		
		$userid = $this->add_user($magv, $tengv, $ngaysinh, $email, $gioitinh, $diachi, $didong, $cmnd, $stt);

		$field2 = array(
				'user_id' => $userid,
		);
		
		$this->db->set($field2);
		$this->db->where('id',$teacherid);
		$this->db->update('giaovien');
		return $teacherid;
	}
	
	// Hàm này tự động tạo mã
	public function auto_code($id)
	{
		$i = 3;
		$len = strlen($id);
		$pre ='';
		for($j=0;$j<$i-$len;$j++)
		{
		$pre .= '0';
		}
		$code = $pre . "$id";
		return $code;
	}
	
	function get_teacher_cong_tac($teacherid)
	{
		$this->db->select('*');
		$this->db->from('giaovien_congtac');
		$this->db->where('id_gv',$teacherid);
		$this->db->order_by('id','desc');
		$query = $this->db->get();
		return $query->result_array();
	}
	
	function them_cong_tac($start_time,$end_time,$noi_dung,$cap_bac, $time, $cap_uy_dang,$teacher_id)
	{
		$field = array(
				'start_time'	=>	$start_time,
				'end_time'		=>	$end_time,
				'noidung'		=>	$noi_dung,
				'capbac'		=>	$cap_bac,
				'time'		=>	$time,
				'capuydang'		=>	$cap_uy_dang,
				'id_gv'			=>	$teacher_id,
		);
		
		$this->db->set($field);
		$this->db->insert('giaovien_congtac');
		return $this->db->insert_id();
	}
	
	function save_cong_tac($id, $start_time,$end_time,$noi_dung,$cap_bac, $time, $cap_uy_dang)
	{
		$field = array(
				'start_time'	=>	$start_time,
				'end_time'		=>	$end_time,
				'noidung'		=>	$noi_dung,
				'capbac'		=>	$cap_bac,
				'time'			=>	$time,
				'capuydang'		=>	$cap_uy_dang,
		);
	
		$this->db->set($field);
		$this->db->where('id',$id);
		return $this->db->update('giaovien_congtac');
	}
	
	// Sửa thông tin giáo viên
	function edit_teacher($teacherid, $user_id, $avatar)
	{
		$tengv = $this->input->post('tengv');
		
		$date1 = str_replace('/', '-', $this->input->post('ngaysinh'));
		$ngaysinh = date('Y-m-d', strtotime($date1));
		
		$date2 = str_replace('/', '-', $this->input->post('ngaytuyendung'));
		$ngaytuyendung = date('Y-m-d', strtotime($date2));
		
		$date3 = str_replace('/', '-', $this->input->post('ngaynhapngu'));
		$ngaynhapngu = date('Y-m-d', strtotime($date3));
		
		$date4 = str_replace('/', '-', $this->input->post('ngayvaodang'));
		$ngayvaodang = date('Y-m-d', strtotime($date4));
		
		$date5 = str_replace('/', '-', $this->input->post('ngaychinhthuc'));
		$ngaychinhthuc = date('Y-m-d', strtotime($date5));
		
		$date6 = str_replace('/', '-', $this->input->post('ngaysinhvochong'));
		$ngaysinhvochong = date('Y-m-d', strtotime($date6));
		
		$email = $this->input->post('email');
		$gioitinh = $this->input->post('gioitinh');
		$didong = $this->input->post('didong');
		$diachi = $this->input->post('diachi');
		$cmnd = $this->input->post('cmnd');
		
		$fields = array(
				'tengv'			=> $tengv,
				'ngaysinh'		=> $ngaysinh,
				'email'			=> $email,
				'gioitinh'		=> $gioitinh,
				'didong'		=> $didong,
				'id_khoa'		=> $this->input->post('makhoa'),
				'diachi'		=> $diachi,
				'cmnd'			=> $cmnd,
				'edited_by' 	=> $this->user->id,
				'tenks' 		=> $this->input->post('tenks'),
				'shsq'			=> $this->input->post('shsq'),
				'capbac'		=> $this->input->post('capbac'),
				'trinhdo'		=> $this->input->post('trinhdo'),
				'nguyenquan' 	=> $this->input->post('nguyenquan'),
				'ngaynhapngu' 	=> $ngaynhapngu,
				'ngaytuyendung' => $ngaytuyendung,
				'ngayvaodang'	=> $ngayvaodang,
				'ngaychinhthuc'	=> $ngaychinhthuc,
				'mucluong'		=> $this->input->post('mucluong'),
				'ngoaingu'		=> $this->input->post('ngoaingu'),
				'suckhoe'		=> $this->input->post('suckhoe'),
				'khenthuong'	=> $this->input->post('khenthuong'),
				'quatruong'		=> $this->input->post('quatruong'),
				'chientruong'	=> $this->input->post('chientruong'),
				'danhhieu'		=> $this->input->post('danhhieu'),
				'kyluat'		=> $this->input->post('kyluat'),
				'dinuocngoai'	=> $this->input->post('dinuocngoai'),
				'nhao'			=> $this->input->post('nhao'),
				'tencha'		=> $this->input->post('tencha'),
				'namsinhcha'	=> $this->input->post('namsinhcha'),
				'nghenghiepcha'	=> $this->input->post('nghenghiepcha'),
				'tenme'			=> $this->input->post('tenme'),
				'namsinhme'		=> $this->input->post('namsinhme'),
				'nghenghiepme'	=> $this->input->post('nghenghiepme'),
				'ghichu1'		=> $this->input->post('ghichu1'),
				'tenchavochong'	=> $this->input->post('tenchavochong'),
				'namsinhchavc'	=> $this->input->post('namsinhchavc'),
				'nghenghiepchavc'	=> $this->input->post('nghenghiepchavc'),
				'tenmevochong'	=> $this->input->post('tenmevochong'),
				'namsinhmevc'	=> $this->input->post('namsinhmevc'),
				'nghenghiepmevc'=> $this->input->post('nghenghiepmevc'),
				'ghichu2'		=> $this->input->post('ghichu2'),
				'tenvochong'	=> $this->input->post('tenvochong'),
				'nghenghiepvochong'	=> $this->input->post('nghenghiepvochong'),
				'ngaysinhvochong'	=> $ngaysinhvochong,
				'choovochong'	=> $this->input->post('choovochong'),
				'concai'		=> $this->input->post('concai'),
				'ghichu3'		=> $this->input->post('ghichu3'),
				'anhdaidien' 	=> $avatar,
		);
		
		$fields2 = array(
				'tengv'			=> $tengv,
				'ngaysinh'		=> $ngaysinh,
				'email'			=> $email,
				'gioitinh'		=> $gioitinh,
				'didong'		=> $didong,
				'id_khoa'		=> $this->input->post('makhoa'),
				'diachi'		=> $diachi,
				'cmnd'			=> $cmnd,
				'edited_by' 	=> $this->user->id,
				'tenks' 		=> $this->input->post('tenks'),
				'shsq'			=> $this->input->post('shsq'),
				'capbac'		=> $this->input->post('capbac'),
				'trinhdo'		=> $this->input->post('trinhdo'),
				'nguyenquan' 	=> $this->input->post('nguyenquan'),
				'ngaynhapngu' 	=> $ngaynhapngu,
				'ngaytuyendung' => $ngaytuyendung,
				'ngayvaodang'	=> $ngayvaodang,
				'ngaychinhthuc'	=> $ngaychinhthuc,
				'mucluong'		=> $this->input->post('mucluong'),
				'ngoaingu'		=> $this->input->post('ngoaingu'),
				'suckhoe'		=> $this->input->post('suckhoe'),
				'khenthuong'	=> $this->input->post('khenthuong'),
				'quatruong'		=> $this->input->post('quatruong'),
				'chientruong'	=> $this->input->post('chientruong'),
				'danhhieu'		=> $this->input->post('danhhieu'),
				'kyluat'		=> $this->input->post('kyluat'),
				'dinuocngoai'	=> $this->input->post('dinuocngoai'),
				'nhao'			=> $this->input->post('nhao'),
				'tencha'		=> $this->input->post('tencha'),
				'namsinhcha'	=> $this->input->post('namsinhcha'),
				'nghenghiepcha'	=> $this->input->post('nghenghiepcha'),
				'tenme'			=> $this->input->post('tenme'),
				'namsinhme'		=> $this->input->post('namsinhme'),
				'nghenghiepme'	=> $this->input->post('nghenghiepme'),
				'ghichu1'		=> $this->input->post('ghichu1'),
				'tenchavochong'	=> $this->input->post('tenchavochong'),
				'namsinhchavc'	=> $this->input->post('namsinhchavc'),
				'nghenghiepchavc'	=> $this->input->post('nghenghiepchavc'),
				'tenmevochong'	=> $this->input->post('tenmevochong'),
				'namsinhmevc'	=> $this->input->post('namsinhmevc'),
				'nghenghiepmevc'=> $this->input->post('nghenghiepmevc'),
				'ghichu2'		=> $this->input->post('ghichu2'),
				'tenvochong'	=> $this->input->post('tenvochong'),
				'nghenghiepvochong'	=> $this->input->post('nghenghiepvochong'),
				'ngaysinhvochong'	=> $ngaysinhvochong,
				'choovochong'	=> $this->input->post('choovochong'),
				'concai'		=> $this->input->post('concai'),
				'ghichu3'		=> $this->input->post('ghichu3'),
		);
		if($avatar==null)
		{
			$this->db->set($fields2);
		}
		else {
			$this->db->set($fields);
		}
		$this->db->where('id',$teacherid);
		$this->db->update('giaovien');

		
		// Cập nhật thông tin người dùng khi thay đổi thông tin của giáo viên
		$field2 = array(
				'fullname' 		=> $tengv,
				'email'			=> $email,
				'mobile'		=> $didong,
				'address'		=> $diachi,
				'role_id'		=> 3,
				'active'		=> 0,
				'birthdate'		=> $ngaysinh,
				'avatar'		=> $avatar,
		);
		$this->db->set($field2);
		$this->db->where('id',$user_id);
		$this->db->update('user');
	}
	
	// Thêm người dùng vào hệ thống
	function add_user($magv, $tengv, $ngaysinh, $email, $gioitinh, $diachi, $didong, $cmnd, $stt)
	{
		
		$password = 'truong19bqp';
		$hash = sha1(microtime());
		$salt = $this->config->item('auth');
		$salt = $salt['salt'];
		$password = sha1($salt.$hash.$password);
		
		$fields = array(
				'fullname' 		=> $tengv,
				'username'		=> 'T19CB'.$magv,
				'email'			=> $email,
				'password'		=> $password,
				'hash'			=> $hash,
				'mobile'		=> $didong,
				'address'		=> $diachi,
				'role_id'		=> 3,
				'active'		=> 1,
				'birthdate'		=> $ngaysinh,
				'deleted'		=> 0,
					
		);
		$this->db->set($fields);		
		$this->db->insert('user');
		return $this->db->insert_id();
	}
	
	public function search_teacher($keyword)
	{
		$this->db->select('giaovien.id, giaovien.magv, giaovien.tengv, giaovien.ngaysinh, giaovien.email, giaovien.didong, giaovien.status, khoa.tenkhoa');
		$this->db->from('giaovien','khoa');
		$this->db->join('khoa','khoa.id = giaovien.id_khoa');
		$this->db->like('magv', $keyword);
		$this->db->or_like('tengv', $keyword);
		$this->db->order_by('id', 'DESC');
		$query = $this->db->get();
			
		if ($query->num_rows() > 0)
		{
			$result = $query->result_array();
			return $result;
		}
		else
		{
			return null;
		}
	}
	
	
	public function check_cmnd()
	{
		$this->db->where('cmnd',$this->input->post('cmnd'));
		$query = $this->db->get('giaovien');
				if($query->num_rows() == 1)
				{
				return true;
		}
				else
				{
				return false;
		}
	}
	
	public function teacher_exchange($id, $status = 0)
	{
		$data = array(
				'status' => $status,
		);
	
		$this->db->where('id', $id);
		$this->db->update('giaovien', $data);
		if($this->db->affected_rows() >= 0)
		{
			return TRUE;
		}
		else
		{
			return FALSE;
		}
	}
	
	function get_all_gv()
	{
		$this->db->select('*');
		$this->db->from('giaovien');
		$this->db->order_by('id','asc');
		$query = $this->db->get();
		return $query->result_array();
	}
	
	public function check_magv()
	{
		$code = $this->input->post('magv');
		$query = "
		SELECT magv
		FROM giaovien WHERE magv ='$code'
		";
		$result = $this->db->query($query);
		if($result->num_rows() >= 1)
		{
		return true;
		}
		else
		{
		return false;
		}
		}
}

/* Location: ./application/models/teacher_model.php */